package com.chuangjiangx.member.manager.basic.web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhoufushou on 2018/6/27.
 */
@Slf4j
public class UniqueNameGenerator extends AnnotationBeanNameGenerator{

    private static Set<String> beanNames = new HashSet<>();

    @Override
    protected String buildDefaultBeanName(BeanDefinition definition) {
        //spring默认使用非限定类名,且首字母小写
        String defaultBeanName = super.buildDefaultBeanName(definition);
        //如果存在相同类名，则使用全限定类名
        if (beanNames.contains(defaultBeanName)) {
            log.info("非限定类名相同，修改使用全限定类名:{}", definition.getBeanClassName());
            return definition.getBeanClassName();
        }
        beanNames.add(defaultBeanName);
        return defaultBeanName;
    }

}
